{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "id": "Fu3OT8EoaUOE"
   },
   "outputs": [],
   "source": [
    "# import packages \n",
    "\n",
    "import numpy as np\n",
    "import os\n",
    "import datetime\n",
    "import time\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import h5py\n",
    "%matplotlib inline\n",
    "\n",
    "three_type = True\n",
    "\n",
    "# folder path and name\n",
    "project_path = os.getcwd()\n",
    "data_folder = os.path.join(project_path,\"data\")\n",
    "pred_folder = os.path.join(data_folder,'npy_before_hdf5')\n",
    "times_folder = os.path.join(data_folder,'video_data')\n",
    "\n",
    "times_5min_trainval = np.load(os.path.join(times_folder,'times_5min_trainval.npy'), allow_pickle=True)\n",
    "image_log_trainval = np.load(os.path.join(pred_folder,'All','image_log_trainval.npy'), allow_pickle=True)\n",
    "image_pred_trainval = np.load(os.path.join(pred_folder,'All','image_pred_trainval.npy'), allow_pickle=True)\n",
    "pv_log_trainval = np.load(os.path.join(pred_folder,'All','pv_log_trainval.npy'), allow_pickle=True)\n",
    "pv_pred_trainval = np.load(os.path.join(pred_folder,'All','pv_pred_trainval.npy'), allow_pickle=True)\n",
    "weather_train_df = pd.read_csv(\"./cloudy_detect/weather_data_train.csv\", parse_dates=[\"timestamp\"])\n",
    "times_5min_test = np.load(os.path.join(times_folder,'times_5min_test.npy'), allow_pickle=True)\n",
    "image_log_test = np.load(os.path.join(pred_folder,'All','image_log_test.npy'), allow_pickle=True)\n",
    "image_pred_test = np.load(os.path.join(pred_folder,'All','image_pred_test.npy'), allow_pickle=True)\n",
    "pv_log_test = np.load(os.path.join(pred_folder,'All','pv_log_test.npy'), allow_pickle=True)\n",
    "pv_pred_test = np.load(os.path.join(pred_folder,'All','pv_pred_test.npy'), allow_pickle=True)\n",
    "weather_test_df = pd.read_csv(\"./cloudy_detect/weather_data_test.csv\", parse_dates=[\"timestamp\"])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "分类保存完成！\n"
     ]
    }
   ],
   "source": [
    "merged_train_df = pd.merge(\n",
    "    pd.DataFrame({'timestamp': times_5min_trainval}),\n",
    "    weather_train_df,\n",
    "    on='timestamp',\n",
    "    how='left'\n",
    ")\n",
    "merged_test_df = pd.merge(\n",
    "    pd.DataFrame({'timestamp': times_5min_test}),\n",
    "    weather_test_df,\n",
    "    on='timestamp',\n",
    "    how='left'\n",
    ")\n",
    "# 4. 获取分类掩码\n",
    "sunny_mask_train = merged_train_df['dominant_weather'] == 'Sunny'\n",
    "cloudy_mask_train = merged_train_df['dominant_weather'] == 'Cloudy'\n",
    "if three_type:\n",
    "    overcast_mask_train = merged_train_df['dominant_weather'] == 'Overcast'\n",
    "sunny_mask_test = merged_test_df['dominant_weather'] == 'Sunny'\n",
    "cloudy_mask_test = merged_test_df['dominant_weather'] == 'Cloudy'\n",
    "if three_type:\n",
    "    overcast_mask_test = merged_test_df['dominant_weather'] == 'Overcast'\n",
    "\n",
    "def save_category(mask, category_name, times, images_log, image_pred, pv_log, pv_pred, is_train=False):\n",
    "    # 提取当前类别的数据\n",
    "    category_times = times[mask]\n",
    "    category_images_log = images_log[mask]\n",
    "    category_images_pred = image_pred[mask]\n",
    "    category_pv_log = pv_log[mask]\n",
    "    category_pv_pred = pv_pred[mask]\n",
    "\n",
    "    if is_train:\n",
    "        np.save(f'./data/data_forecast/{category_name}/times_trainval.npy',category_times)\n",
    "        np.save(os.path.join(pred_folder,category_name,'image_log_trainval.npy'), category_images_log)\n",
    "        np.save(os.path.join(pred_folder,category_name,'image_pred_trainval.npy'), category_images_pred)\n",
    "        np.save(os.path.join(pred_folder,category_name,'pv_log_trainval.npy'), category_pv_log)\n",
    "        np.save(os.path.join(pred_folder,category_name,'pv_pred_trainval.npy'),category_pv_pred)\n",
    "    else:\n",
    "        np.save(f'./data/data_forecast/{category_name}/times_test.npy',category_times)\n",
    "        np.save(os.path.join(pred_folder,category_name,'image_log_test.npy'), category_images_log)\n",
    "        np.save(os.path.join(pred_folder,category_name,'image_pred_test.npy'), category_images_pred)\n",
    "        np.save(os.path.join(pred_folder,category_name,'pv_log_test.npy'), category_pv_log)\n",
    "        np.save(os.path.join(pred_folder,category_name,'pv_pred_test.npy'),category_pv_pred)\n",
    "\n",
    "save_category(sunny_mask_train, \"Sunny\", times_5min_trainval, image_log_trainval, image_pred_trainval, pv_log_trainval, pv_pred_trainval, True)\n",
    "save_category(cloudy_mask_train, \"Cloudy\", times_5min_trainval, image_log_trainval, image_pred_trainval, pv_log_trainval, pv_pred_trainval, True)\n",
    "if three_type:\n",
    "    save_category(overcast_mask_train, \"Overcast\", times_5min_trainval, image_log_trainval, image_pred_trainval, pv_log_trainval, pv_pred_trainval, True)\n",
    "save_category(sunny_mask_test, \"Sunny\", times_5min_test, image_log_test, image_pred_test, pv_log_test, pv_pred_test, False)\n",
    "save_category(cloudy_mask_test, \"Cloudy\", times_5min_test, image_log_test, image_pred_test, pv_log_test, pv_pred_test, False)\n",
    "if three_type:\n",
    "    save_category(overcast_mask_test, \"Overcast\", times_5min_test, image_log_test, image_pred_test, pv_log_test, pv_pred_test, False)\n",
    "print(\"分类保存完成！\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def create_h5py(base_path, weather_type):\n",
    "    # 更新后的 np_files 字典，包含 images_pred\n",
    "    np_files = {\n",
    "        'trainval': [\n",
    "            base_path + 'pv_log_trainval.npy', \n",
    "            base_path + 'pv_pred_trainval.npy', \n",
    "            base_path + 'image_log_trainval.npy', \n",
    "            base_path + 'image_pred_trainval.npy'  # 新增 images_pred\n",
    "        ],\n",
    "        'test': [\n",
    "            base_path + 'image_log_test.npy', \n",
    "            base_path + 'image_pred_test.npy',     # 新增 images_pred\n",
    "            base_path + 'pv_log_test.npy', \n",
    "            base_path + 'pv_pred_test.npy'\n",
    "        ]\n",
    "    }\n",
    "\n",
    "    # 创建 HDF5 文件并写入数据\n",
    "    with h5py.File(f'./data/data_forecast/{weather_type}/forecast.hdf5', 'w') as f:\n",
    "        for group_name, files in np_files.items():\n",
    "            group = f.create_group(group_name)  # 创建组（trainval 或 test）\n",
    "            for file in files:\n",
    "                if os.path.exists(file):  # 检查文件是否存在\n",
    "                    data = np.load(file)  # 加载 NumPy 数据\n",
    "                    dataset_name = os.path.splitext(os.path.basename(file))[0]  # 提取文件名作为数据集名称\n",
    "                    group.create_dataset(dataset_name, data=data)  # 在组中创建数据集\n",
    "                else:\n",
    "                    print(f\"Warning: File {file} does not exist. Skipping...\")\n",
    "create_h5py(\"./data/npy_before_hdf5/Sunny/\", \"Sunny\")\n",
    "create_h5py(\"./data/npy_before_hdf5/Cloudy/\", \"Cloudy\")\n",
    "if three_type:\n",
    "    create_h5py(\"./data/npy_before_hdf5/Overcast/\", \"Overcast\")"
   ]
  }
 ],
 "metadata": {
  "colab": {
   "collapsed_sections": [],
   "name": "data_preprocess_forecast.ipynb",
   "provenance": []
  },
  "kernelspec": {
   "display_name": "solar",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
